package com.aidex.biz.controller;

import com.aidex.biz.domain.BizZjwzInfo;
import com.aidex.biz.service.BizZjwzInfoService;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.core.domain.AjaxResult;
import com.aidex.common.core.domain.R;
import com.aidex.common.core.page.PageDomain;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.SecurityUtils;
import com.aidex.common.utils.poi.ExcelUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 执纪问责Controller
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2025-10-14
 */
@RestController
@RequestMapping("/biz/bizZjwzInfo")
public class BizZjwzInfoController extends BaseController {
    @Autowired
    private BizZjwzInfoService bizZjwzInfoService;

    /**
     * 查询执纪问责列表
     */
    @GetMapping("/list")
    public R<PageInfo> list(BizZjwzInfo bizZjwzInfo, HttpServletRequest request, HttpServletResponse response) {
        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            bizZjwzInfo.setCompanyId(null);
        } else {
            bizZjwzInfo.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        bizZjwzInfo.setPage(new PageDomain(request, response));
        return R.data(bizZjwzInfoService.findPage(bizZjwzInfo));
    }

    /**
     * 获取执纪问责详细信息
     */
    @GetMapping(value = "/{id}")
    public R<BizZjwzInfo> detail(@PathVariable("id") String id) {
        return R.data(bizZjwzInfoService.get(id));
    }

    /**
     * 新增执纪问责
     */
    @Log(title = "执纪问责", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated BizZjwzInfo bizZjwzInfo) {
        return R.status(bizZjwzInfoService.save(bizZjwzInfo));
    }

    /**
     * 修改执纪问责
     */
    @Log(title = "执纪问责", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated BizZjwzInfo bizZjwzInfo) {
        return R.status(bizZjwzInfoService.save(bizZjwzInfo));
    }


    /**
     * 删除执纪问责
     */
    @Log(title = "执纪问责", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(bizZjwzInfoService.deleteBizZjwzInfoByIds(ids));
    }

    /**
     * 获取最大编号
     */
    @GetMapping("/findMaxSort")
    public R findMaxSort() {
        return R.data(bizZjwzInfoService.findMaxSort(new BizZjwzInfo()));
    }

    /**
     * 导出执纪问责列表
     */
    @Log(title = "执纪问责", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(BizZjwzInfo bizZjwzInfo) {

        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            bizZjwzInfo.setCompanyId(null);
        } else {
            bizZjwzInfo.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        List<BizZjwzInfo> list = bizZjwzInfoService.findList(bizZjwzInfo);
        ExcelUtil<BizZjwzInfo> util = new ExcelUtil<BizZjwzInfo>(BizZjwzInfo.class);
        return util.exportExcel(list, "执纪问责数据");
    }

    @Log(title = "执纪问责导入", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<BizZjwzInfo> util = new ExcelUtil<BizZjwzInfo>(BizZjwzInfo.class);
        List<BizZjwzInfo> bizZjwzInfoList = util.importExcel(file.getInputStream());
        String message = bizZjwzInfoService.importData(bizZjwzInfoList);
        return AjaxResult.success(message);
    }


}
